import java.io.*;
import java.util.Scanner;

public class file_copy {
    public static void main(String[] args) throws IOException {
        Scanner middle=new Scanner(System.in);
        String startIp,endIp;
        //创建字符串，用于输入输出路径
        System.out.println("请输入需要复制的文件路径:");
       while(true){//循环直到输入路径正确；"C:\Users\86186\OneDrive\桌面\666.txt"
           startIp=middle.nextLine();
           File file1=new File(startIp);
           if(file1.isFile()){
               System.out.println("文件路径输入成功，请继续操作");
               break;
           }
           else{
               if(file1.isDirectory()){
                   System.out.println("该路径为文件夹，请检查路径并重新输入");
                   continue;
               }
               System.out.println("文件不存在，请重新输入有效文件路径:");
           }
       }
       byte[] chars=new byte[100];
       int len;//为下面复制做准备；
       System.out.println("请输入被复制文件路径");
       endIp=middle.nextLine();
       File file2=new File(endIp);
       while(true){
           if(file2.isDirectory()){
               System.out.println("该文件为文件夹，请重新输入文件路径");
               continue;
           }
           if(!file2.isFile()){
               FileInputStream file1_input=new FileInputStream(startIp);
               FileOutputStream file2_out=new FileOutputStream(endIp);
               while((len=file1_input.read(chars))!=-1){
                       file2_out.write(chars,0,len);
               }
               System.out.println("已为您新建文件并复制成功，文件路径为"+endIp);
               break;
           }
           else {
               System.out.println("覆盖已有文件请输入1\n续写请按2");
               int i=middle.nextInt();//续写后者覆盖原文件；
               boolean tf=i==1?true:false;
               FileInputStream file1_input=new FileInputStream(startIp);
               FileOutputStream file2_out=new FileOutputStream(endIp,tf);
               while((len=file1_input.read(chars))!=-1){
                   file2_out.write(chars,0,len);
               }
               System.out.println("执行成功！");
               break;
           }
       }

    }
}
